package com.pengxun.manager.controller.system;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pengxun.manager.model.system.UserRole;
import com.pengxun.manager.model.system.Userinfo;
import com.pengxun.manager.service.system.LoginService;
import com.pengxun.manager.service.system.RoleMenuService;
import com.pengxun.manager.service.system.UserRoleService;
import com.pengxun.manager.vo.AssignRolesVO;
import com.pengxun.manager.vo.UserPermissionVO;
import com.szeastroc.common.exception.EastrocException;
import com.szeastroc.common.jdbc.page.Page;
import com.szeastroc.common.result.Result;
import com.szeastroc.common.result.ResultUtils;

@RestController
@RequestMapping("/userRole")
public class UserRoleController  {

	@Resource
	private UserRoleService userRoleService;
	
	@Resource
	private LoginService loginService;
	
	@Resource
	private RoleMenuService roleMenuService;

	@RequestMapping("/insert")
	public Result insert(@RequestBody UserRole userRole) {
		try {
			userRoleService.insert(userRole);
		} catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}
		return ResultUtils.render();
	};

	@RequestMapping("/update")
	public Result update(@RequestBody UserRole userRole) {
		try {
			userRoleService.update(userRole);
		} catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}
		return ResultUtils.render();
	};

	@RequestMapping("/delete")
	public Result delete(Integer id) {
		try {
			userRoleService.delete(id);
		} catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}
		return ResultUtils.render();
	};

	@RequestMapping("/findByPage")
	public Result findByPage(Page page) {
		page = userRoleService.findByPage(page);
		return ResultUtils.render(page);
	};
	
	@RequestMapping("/findByUserId")
	public Result findByPage(Long userId) {
		List<UserRole> list = userRoleService.findByUserId(userId);
		return ResultUtils.render(list);
	};
	
	@RequestMapping("/getUserPermission")
	public Result getUserPermission() {
		try{
			Userinfo userInfo = loginService.getCurrentUser();
			UserPermissionVO UserPermission = roleMenuService.getUserPermission(userInfo.getUserId());
			return ResultUtils.render(UserPermission);
		}catch (EastrocException e) {
			e.printStackTrace();
			return ResultUtils.renderError(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtils.renderError(e.getMessage());
		}
		
	};

	@RequestMapping("/assignRoles")
	public Result assignRoles(@RequestBody  AssignRolesVO assignRolesVO){
		try {
			if (null == assignRolesVO || null == assignRolesVO.getUserId()
					|| null==assignRolesVO.getRoleCodes() || assignRolesVO.getRoleCodes().size() == 0 ){
				return ResultUtils.renderError("参数异常");
			}
			userRoleService.assignRoles(assignRolesVO);
			return ResultUtils.render();
		}catch (Exception e){
			return ResultUtils.renderError(e.getMessage());
		}
	}
	
}
